package com.realtyreg.webview.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import com.realtyreg.domain.entity.realty.Realty;
import com.realtyreg.service.RealtyService;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Controller
@RequestMapping("/realty")
public class RealtyController {
    @Autowired
    private RealtyService realtyService;

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    //список
    @RequestMapping(value = "", method = RequestMethod.GET)
    public String list(Model model) {
        model.addAttribute("realtyList", realtyService.findAll());
        return "realtyList";
    }

    //просмотр
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String view(@PathVariable String id, Model model) {
        Realty realty = realtyService.findOne(Long.valueOf(id));
        model.addAttribute(realty);
        return "realtyView";
    }

    //добавление GET
    @RequestMapping(value = "", params = "form", method = RequestMethod.GET)
    public String createForm(Model model) {
        Realty realty = new Realty();
        model.addAttribute(realty);
        return "realtyEdit";
    }

    //добавление POST
    @RequestMapping(value = "", params = "form",method = RequestMethod.POST)
    public String create(@Valid @ModelAttribute Realty realty, BindingResult result, SessionStatus status) {
        //todo realty.createDate is null
        if (result.hasErrors()) {
            return "realtyEdit";
        }
        realty.setCreateDate(Calendar.getInstance());
        realtyService.save(realty);
        status.setComplete();
        return "redirect:";
    }

    //редактирование GET
    @RequestMapping(value = "/{id}", params = "form",method = RequestMethod.GET)
    public String updateForm(@PathVariable String id, Model model) {
        Realty realty = realtyService.findOne(Long.valueOf(id));
        model.addAttribute(realty);
        return "realtyEdit";
    }

    //редактирование POST
    @RequestMapping(value = "/{id}", params = "form",method = RequestMethod.POST)
    public String update(@PathVariable String id, @Valid @ModelAttribute Realty realty, BindingResult result, SessionStatus status) {
        //todo realty.createDate is null
        if (result.hasErrors()) {
            return "realtyEdit";
        }
        realty.setCreateDate(Calendar.getInstance());
        realtyService.save(realty);
        status.setComplete();
        return "redirect:/realty";
    }
}
